/*
 * Project: babylon
 * 
 * File Created at 2011-7-26
 * 
 * Copyright 2011 Alibaba.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */

package com.whale.widget;

import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

/**
 * 监听EditView中的内容变化
 * @author qibao.xieqb
 *
 */
public class ListenerEdit {
    private EditText mEditText;
    private TextView mCharsRemainText;
    
    private int mLabelType = 0; //  0:倒数     1：1/500样式
    
    private static final int MAX_FEED_INPUT_LENGTH = 500;
  
    public ListenerEdit(EditText editText, TextView charsRemainText) {
        mEditText = editText;
        mCharsRemainText = charsRemainText;
        mEditText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(
              MAX_FEED_INPUT_LENGTH) });        
     }  
    
    public ListenerEdit(EditText editText, TextView charsRemainText, int labelType) {
        mEditText = editText;
        mCharsRemainText = charsRemainText;
        mEditText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(
              MAX_FEED_INPUT_LENGTH) });
        mLabelType = labelType;
    }  
    
     private TextWatcher mTextWatcher = new TextWatcher() {
        @Override
        public void afterTextChanged(Editable e) {
          updateCharsRemain();
        }
    
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        }
    
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
     };
    
     public void updateCharsRemain() {
         if(mLabelType == 0) {
             int remaining = MAX_FEED_INPUT_LENGTH - mEditText.length();
             mCharsRemainText.setText(remaining + "");
         } else {
             mCharsRemainText.setText(mEditText.length() +  " / 500");
         }
     }
      
     public void addTextChangedListener(){
          mEditText.addTextChangedListener(mTextWatcher);
     }
     
     public void setText(String text) {
         mEditText.setText(text);
         updateCharsRemain();
     }
     
     public String getText() {
         return mEditText.getText().toString();
     }
}
